package com.zzyl.nursing.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.Constants;
import com.zzyl.common.core.redis.RedisCache;
import com.zzyl.nursing.domain.NursingLevel;
import com.zzyl.nursing.mapper.NursingLevelMapper;
import com.zzyl.nursing.service.INursingLevelService;
import com.zzyl.nursing.vo.NursingLevelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 护理等级Service业务层处理
 * 
 * @author alexis
 * @date 2024-12-30
 */
@Service
public class NursingLevelServiceImpl extends ServiceImpl<NursingLevelMapper, NursingLevel> implements INursingLevelService
{
    @Autowired
    private NursingLevelMapper nursingLevelMapper;
    @Autowired
    private RedisCache redisCache;


//    private static final String CACHE_KEY_PREFIX = "nursingLevel:all";
    /**
     * 查询护理等级
     * 
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Long id)
    {
        return nursingLevelMapper.selectById(id);
    }

    /**
     * 查询护理等级列表
     * 
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevel> selectNursingLevelList(NursingLevel nursingLevel)
    {

        return nursingLevelMapper.selectNursingLevelList(nursingLevel);
    }

    /**
     * 新增护理等级
     * 
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevel nursingLevel)
    {
        int row = nursingLevelMapper.insert(nursingLevel);
        try {
            redisCache.deleteObject(Constants.CACHE_NURSING_LEVEL_PREFIX);
        } catch (Exception e) {
            log.error("新增护理等级后删除redis护理等级信息失败");
        }
        return row;
    }

    /**
     * 修改护理等级
     * 
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel)
    {
        int row = nursingLevelMapper.updateById(nursingLevel);
        try {
            redisCache.deleteObject(Constants.CACHE_NURSING_LEVEL_PREFIX);
        } catch (Exception e) {
            log.error("修改护理等级后删除redis护理等级信息失败");
        }
        return row;
    }

    /**
     * 批量删除护理等级
     * 
     * @param ids 需要删除的护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelByIds(Long[] ids)
    {
        int row = nursingLevelMapper.deleteBatchIds(Arrays.asList(ids));
        try {
            redisCache.deleteObject(Constants.CACHE_NURSING_LEVEL_PREFIX);
        } catch (Exception e) {
            log.error("删除redis护理等级信息失败");
        }
        return row;
    }

    /**
     * 删除护理等级信息
     * 
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Long id)
    {
        int row = nursingLevelMapper.deleteById(id);
        try {
            redisCache.deleteObject(Constants.CACHE_NURSING_LEVEL_PREFIX);
        } catch (Exception e) {
            log.error("删除redis护理等级信息失败");
        }
        return row;
    }

    /**
     * 查询护理等级Vo列表
     *
     * @param nursingLevel 条件
     * @return 结果
     */
    @Override
    public List<NursingLevelVo> selectNursingLevelVoList(NursingLevel nursingLevel) {
        return nursingLevelMapper.selectNursingLevelVoList(nursingLevel);
    }

    @Override
    public List<NursingLevel> listAll() {
        List<NursingLevel> nursingLevelList;
        try {
            nursingLevelList= redisCache.getCacheObject(Constants.CACHE_NURSING_LEVEL_PREFIX);
            if(CollUtil.isNotEmpty(nursingLevelList)){
                return nursingLevelList;
            }
        } catch (Exception e) {
            log.error("从Redis中获取护理等级数据失败");
        }
        LambdaQueryWrapper<NursingLevel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NursingLevel::getStatus, 1);
        List<NursingLevel> list = this.list(wrapper);
        try {
            redisCache.setCacheObject(Constants.CACHE_NURSING_LEVEL_PREFIX,list,24 * 60 * 60, TimeUnit.SECONDS);
        } catch (Exception e) {
            log.error("将护理等级数据缓存到Redis失败");
        }
        return list;
    }
}
